package com.danger.Activity.Account;

import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.danger.Activity.BackHandledFragment;
import com.danger.Activity.R;
import com.danger.Model.bean.RegisterUserInfo;
import com.danger.Presenter.RegisterPresenter;
import com.danger.Presenter.RegisterPresenterImpl;
import com.danger.View.RegisterView;

/**
 * Created by danger on 2016/4/24.
 */
public class RegisterFragment extends BackHandledFragment implements RegisterView, View.OnClickListener {

    private boolean hadIntercept;
    private View view;
    private EditText register_username_editText;
    private EditText register_password_editText;
    private EditText register_check_password_editText;
    private EditText register_qq_editText;
    private EditText register_email_editText;
    private Button register_next_button;
    private Toolbar toolbar;
    private RegisterPresenter registerPresenter;
    private AccountActivity activity;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_register, container, false);
        initView();
        initEvent();
        return view;
    }

    private void initView() {
        activity = (AccountActivity) getActivity();
        toolbar = activity.getToolbar();
        toolbar.setTitle("");
        activity.getToolbarTitle().setText("注册");
        toolbar.setNavigationIcon(R.mipmap.back);
        activity.setSupportActionBar(toolbar);
        activity.getSupportActionBar().setDefaultDisplayHomeAsUpEnabled(true);
        registerPresenter = new RegisterPresenterImpl(activity, this);
        register_username_editText = (EditText) view.findViewById(R.id.register_username_editText);
        register_password_editText = (EditText) view.findViewById(R.id.register_password_editText);
        register_check_password_editText = (EditText) view.findViewById(R.id.register_check_password_editText);
        register_qq_editText = (EditText) view.findViewById(R.id.register_qq_editText);
        register_email_editText = (EditText) view.findViewById(R.id.register_email_editText);
        register_next_button = (Button) view.findViewById(R.id.register_next_button);
    }

    private void initEvent() {
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getFragmentManager().popBackStack();
            }
        });
        register_next_button.setOnClickListener(this);
    }

    @Override
    public void showError(String msg) {
        Toast.makeText(activity, msg, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void toNext() {
        UploadPhotoFragment fragment = new UploadPhotoFragment();
        Bundle bundle = new Bundle();
        bundle.putString("username", register_username_editText.getText().toString());
        bundle.putString("password", register_password_editText.getText().toString());
        bundle.putString("qq", register_qq_editText.getText().toString());
        bundle.putString("email", register_email_editText.getText().toString());
        fragment.setArguments(bundle);
        getFragmentManager().beginTransaction().addToBackStack(null).replace(R.id.account_container, fragment).commit();
    }

    @Override
    public RegisterUserInfo getRegisterUserInfo() {
        RegisterUserInfo registerUserInfo = new RegisterUserInfo();
        registerUserInfo.setUsername(register_username_editText.getText().toString());
        registerUserInfo.setPassword(register_password_editText.getText().toString());
        registerUserInfo.setCheck_password(register_check_password_editText.getText().toString());
        registerUserInfo.setQq(register_qq_editText.getText().toString());
        registerUserInfo.setEmail(register_email_editText.getText().toString());
        return registerUserInfo;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.register_next_button:
                registerPresenter.toNext();
                break;
            default:
                break;
        }
    }

    @Override
    public boolean onBackPressed() {
        if (hadIntercept) {
            return false;
        } else {
            hadIntercept = true;
            return true;
        }
    }
}
